//
//  AuditInfoController.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/21.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
class AuditInfoController: BaseController {
    var carAudit:CarAuditModel!
    var auditInfoTable:UITableView!//数据表格
    var infoCell:CarAuditInfoCell!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: "审车办理", isback: true, appearWay: 3, isright: false)//自定义导航
        if reachability.isReachable {
            getAuditInfo()
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
        //初始化表格
        auditInfoTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH - kStatusBarH - kNavigationBarH),style: UITableViewStyle.plain)
        auditInfoTable?.delegate = self
        auditInfoTable?.dataSource = self
        auditInfoTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        auditInfoTable?.rowHeight = 3*oImgH + 2*oLabelH + 6*oTop + remarkH + cellH + 10
        if "0" == carAudit.oprocess {//不可审核
            auditInfoTable?.rowHeight = 3*oImgH + 2*oLabelH + 7*oTop + 2*remarkH + cellH + 10
        }
        if "1" == carAudit.oprocess {//可审核
            auditInfoTable?.rowHeight = 3*oImgH + 2*oLabelH + 10*oTop + 2*remarkH + 4*cellH + 10
        }
        self.view.addSubview(auditInfoTable)
    }
}
extension AuditInfoController{
    //查询审车详情
    func getAuditInfo(){
        let urlString = "\(projectUrl)/order/other/caraudit/\(carAudit.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
        Alamofire.request(urlString, method: .get, parameters: nil, headers : headers).responseJSON { (response) in
            if response.result.isSuccess{
                let json = JSON(response.result.value as! [String : AnyObject])
                if json["success"] == true {
                    self.carAudit = CarAuditModel.init(dict: json["rdata"])
                    self.auditInfoTable.reloadData()
                }else{
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }else{
                print(response.result.error!) //输出错误日志
            }
        }
    }
    //选择操作方式 支付，抢单，完成订单？
    func operatorFun(sender: UIButton){
        if 0 == sender.tag {//去支付
            let pc = PayController()
            pc.orderID = carAudit.orderNo
            pc.totalAmount = carAudit.samount
            pc.modelType = "caraudit"
            self.present(pc, animated: true, completion: nil)
        }else if(1 == sender.tag){//去抢单
            let urlString = "\(projectUrl)/order/other/accept/caraudit/\(carAudit.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            toOperatOrder(url: urlString)
        }
        else if(2 == sender.tag){//去完成订单
            let urlString = "\(projectUrl)/order/other/finish/caraudit/\(carAudit.orderNo)/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"
            toOperatOrder(url: urlString)
        }
    }
}
extension AuditInfoController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        infoCell = tableView.dequeueReusableCell(withIdentifier: "carAuditInfoCellID") as? CarAuditInfoCell
        if infoCell == nil {
            infoCell = CarAuditInfoCell(style: UITableViewCellStyle.default,reuseIdentifier: "carAuditInfoCellID")
        }
        infoCell.configCellDateWithModel(carAudit)
        return infoCell!
    }
}

